home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / timidsrc.zip / playmidi.c < prev    next >
C/C++ Source or Header  |  1996-05-20  |  23KB  |  978 lines

  1. /*
  2.  
  3.     TiMidity -- Experimental MIDI to WAVE converter
  4.     Copyright (C) 1995 Tuukka Toivonen <toivonen@clinet.fi>
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 2 of the License, or
  9.     (at your option) any later version.
  10.  
  11.     This program is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with this program; if not, write to the Free Software
  18.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.     playmidi.c -- random stuff in need of rearrangement
  21.  
  22. */
  23.  
  24. #include <stdio.h>
  25. #ifndef __WIN32__
  26. #include <unistd.h>
  27. #endif
  28. #include <stdlib.h>
  29.  
  30. #if defined(SOLARIS) || defined(__WIN32__)
  31. # include <string.h>
  32. #else
  33. #include <strings.h>
  34. #endif
  35.  
  36. #include "config.h"
  37. #include "common.h"
  38. #include "instrum.h"
  39. #include "playmidi.h"
  40. #include "readmidi.h"
  41. #include "output.h"
  42. #include "mix.h"
  43. #include "controls.h"
  44.  
  45. #include "tables.h"
  46.  
  47. #ifdef __WIN32__
  48. extern int intr;
  49. #endif
  50.  
  51. Channel channel[16];
  52. Voice voice[MAX_VOICES];
  53.  
  54. int
  55.     voices=DEFAULT_VOICES;
  56.  
  57. int32
  58.     control_ratio=0,
  59.     amplification=DEFAULT_AMPLIFICATION;
  60.  
  61. float
  62.     master_volume;
  63.  
  64. int32 drumchannels=DEFAULT_DRUMCHANNELS;
  65. int adjust_panning_immediately=0;
  66.  
  67. static int32 lost_notes, cut_notes;
  68. static int32 common_buffer[AUDIO_BUFFER_SIZE*2], /* stereo samples */
  69.              *buffer_pointer;
  70. static int32 buffered_count;
  71.  
  72. static MidiEvent *event_list, *current_event;
  73. static int32 sample_count, current_sample;
  74.  
  75. static void adjust_amplification(void)
  76.   master_volume = (double)(amplification) / 100.0L;
  77. }
  78.  
  79. static void reset_voices(void)
  80. {
  81.   int i;
  82.   for (i=0; i<MAX_VOICES; i++)
  83.     voice[i].status=VOICE_FREE;
  84. }
  85.  
  86. /* Process the Reset All Controllers event */
  87. static void reset_controllers(int c)
  88. {
  89.   channel[c].volume=90; /* Some standard says, although the SCC docs say 0. */
  90.   channel[c].expression=127; /* SCC-1 does this. */
  91.   channel[c].sustain=0;
  92.   channel[c].pitchbend=0x2000;
  93.   channel[c].pitchfactor=0; /* to be computed */
  94. }
  95.  
  96. static void redraw_controllers(int c)
  97. {
  98.   ctl->volume(c, channel[c].volume);
  99.   ctl->expression(c, channel[c].expression);
  100.   ctl->sustain(c, channel[c].sustain);
  101.   ctl->pitch_bend(c, channel[c].pitchbend);
  102. }
  103.  
  104. static void reset_midi(void)
  105. {
  106.   int i;
  107.   for (i=0; i<16; i++)
  108.     {
  109.       reset_controllers(i);
  110.       /* The rest of these are unaffected by the Reset All Controllers event */
  111.       channel[i].program=default_program;
  112.       channel[i].panning=NO_PANNING;
  113.       channel[i].pitchsens=2;
  114.       channel[i].bank=0; /* tone bank or drum set */
  115.     }
  116.   reset_voices();
  117. }
  118.  
  119. static void select_sample(int v, Instrument *ip)
  120. {
  121.   int32 f, cdiff, diff;
  122.   int s,i;
  123.   Sample *sp, *closest;
  124.  
  125.   s=ip->samples;
  126.   sp=ip->sample;
  127.  
  128.   if (s==1)
  129.     {
  130.       voice[v].sample=sp;
  131.       return;
  132.     }
  133.  
  134.   f=voice[v].orig_frequency;
  135.   for (i=0; i<s; i++)
  136.     {
  137.       if (sp->low_freq <= f && sp->high_freq >= f)
  138.     {
  139.       voice[v].sample=sp;
  140.       return;
  141.     }
  142.       sp++;
  143.     }
  144.  
  145.   /* 
  146.      No suitable sample found! We'll select the sample whose root
  147.      frequency is closest to the one we want. (Actually we should
  148.      probably convert the low, high, and root frequencies to MIDI note
  149.      values and compare those.) */
  150.  
  151.   cdiff=0x7FFFFFFF;
  152.   closest=sp=ip->sample;
  153.   for(i=0; i<s; i++)
  154.     {
  155.       diff=sp->root_freq - f;
  156.       if (diff<0) diff=-diff;
  157.       if (diff<cdiff)
  158.     {
  159.       cdiff=diff;
  160.       closest=sp;
  161.     }
  162.       sp++;
  163.     }
  164.   voice[v].sample=closest;
  165.   return;
  166. }
  167.  
  168. static void recompute_freq(int v)
  169. {
  170.   int 
  171.     sign=(voice[v].sample_increment < 0), /* for bidirectional loops */
  172.     pb=channel[voice[v].channel].pitchbend;
  173.   double a;
  174.   
  175.   if (!voice[v].sample->sample_rate)
  176.     return;
  177.  
  178.   if (voice[v].vibrato_control_ratio)
  179.     {
  180.       /* This instrument has vibrato. Invalidate any precomputed
  181.          sample_increments. */
  182.  
  183.       int i=VIBRATO_SAMPLE_INCREMENTS;
  184.       while (i--)
  185.     voice[v].vibrato_sample_increment[i]=0;
  186.     }
  187.  
  188.   if (pb==0x2000 || pb<0 || pb>0x3FFF)
  189.     voice[v].frequency=voice[v].orig_frequency;
  190.   else
  191.     {
  192.       pb-=0x2000;
  193.       if (!(channel[voice[v].channel].pitchfactor))
  194.     {
  195.       /* Damn. Somebody bent the pitch. */
  196.       int32 i=pb*channel[voice[v].channel].pitchsens;
  197.       if (pb<0)
  198.         i=-i;
  199.       channel[voice[v].channel].pitchfactor=
  200.         bend_fine[(i>>5) & 0xFF] * bend_coarse[i>>13];
  201.     }
  202.       if (pb>0)
  203.     voice[v].frequency=
  204.       (int32)(channel[voice[v].channel].pitchfactor *
  205.           (double)(voice[v].orig_frequency));
  206.       else
  207.     voice[v].frequency=
  208.       (int32)((double)(voice[v].orig_frequency) /
  209.           channel[voice[v].channel].pitchfactor);
  210.     }
  211.  
  212.   a = FSCALE(((double)(voice[v].sample->sample_rate) *
  213.           (double)(voice[v].frequency)) /
  214.          ((double)(voice[v].sample->root_freq) *
  215.           (double)(play_mode->rate)),
  216.          FRACTION_BITS);
  217.  
  218.   if (sign) 
  219.     a = -a; /* need to preserve the loop direction */
  220.  
  221.   voice[v].sample_increment = (int32)(a);
  222. }
  223.  
  224. static void recompute_amp(int v)
  225. {
  226.   int32 tempamp;
  227.  
  228.   /* TODO: use fscale */
  229.  
  230.   tempamp= (voice[v].velocity *
  231.         channel[voice[v].channel].volume * 
  232.         channel[voice[v].channel].expression); /* 21 bits */
  233.  
  234.   if (!(play_mode->encoding & PE_MONO))
  235.     {
  236.       if (voice[v].panning > 60 && voice[v].panning < 68)
  237.     {
  238.       voice[v].panned=PANNED_CENTER;
  239.  
  240.       voice[v].left_amp=
  241.         FSCALENEG((double)(tempamp) * voice[v].sample->volume * master_volume,
  242.               21);
  243.     }
  244.       else if (voice[v].panning<5)
  245.     {
  246.       voice[v].panned = PANNED_LEFT;
  247.  
  248.       voice[v].left_amp=
  249.         FSCALENEG((double)(tempamp) * voice[v].sample->volume * master_volume,
  250.               20);
  251.     }
  252.       else if (voice[v].panning>123)
  253.     {
  254.       voice[v].panned = PANNED_RIGHT;
  255.  
  256.       voice[v].left_amp= /* left_amp will be used */
  257.         FSCALENEG((double)(tempamp) * voice[v].sample->volume * master_volume,
  258.               20);
  259.     }
  260.       else
  261.     {
  262.       voice[v].panned = PANNED_MYSTERY;
  263.  
  264.       voice[v].left_amp=
  265.         FSCALENEG((double)(tempamp) * voice[v].sample->volume * master_volume,
  266.               27);
  267.       voice[v].right_amp=voice[v].left_amp * (voice[v].panning);
  268.       voice[v].left_amp *= (double)(127-voice[v].panning);
  269.     }
  270.     }
  271.   else
  272.     {
  273.       voice[v].panned=PANNED_CENTER;
  274.  
  275.       voice[v].left_amp=
  276.     FSCALENEG((double)(tempamp) * voice[v].sample->volume * master_volume,
  277.           21);
  278.     }
  279. }
  280.  
  281. static void start_note(MidiEvent *e, int i)
  282. {
  283.   Instrument *ip;
  284.   int j;
  285.  
  286.   if (ISDRUMCHANNEL(e->channel))
  287.     {
  288.       if (!(ip=drumset[channel[e->channel].bank]->tone[e->a].instrument))
  289.     {
  290.       if (!(ip=drumset[0]->tone[e->a].instrument))
  291.         return; /* No instrument? Then we can't play. */
  292.     }
  293.       if (ip->samples != 1)
  294.     {
  295.       ctl->cmsg(CMSG_WARNING, VERB_VERBOSE, 
  296.            "Strange: percussion instrument with %d samples!", ip->samples);
  297.     }
  298.  
  299.       if (ip->sample->note_to_use) /* Do we have a fixed pitch? */
  300.     voice[i].orig_frequency=freq_table[(int)(ip->sample->note_to_use)];
  301.       else
  302.     voice[i].orig_frequency=freq_table[e->a & 0x7F];
  303.       
  304.       /* drums are supposed to have only one sample */
  305.       voice[i].sample=ip->sample;
  306.     }
  307.   else
  308.     {
  309.       if (channel[e->channel].program==SPECIAL_PROGRAM)
  310.     ip=default_instrument;
  311.       else if (!(ip=tonebank[channel[e->channel].bank]->
  312.          tone[channel[e->channel].program].instrument))
  313.     {
  314.       if (!(ip=tonebank[0]->tone[channel[e->channel].program].instrument))
  315.         return; /* No instrument? Then we can't play. */
  316.     }
  317.  
  318.       if (ip->sample->note_to_use) /* Fixed-pitch instrument? */
  319.     voice[i].orig_frequency=freq_table[(int)(ip->sample->note_to_use)];
  320.       else
  321.     voice[i].orig_frequency=freq_table[e->a & 0x7F];
  322.       select_sample(i, ip);
  323.     }
  324.  
  325.   voice[i].status=VOICE_ON;
  326.   voice[i].channel=e->channel;
  327.   voice[i].note=e->a;
  328.   voice[i].velocity=e->b;
  329.   voice[i].sample_offset=0;
  330.   voice[i].sample_increment=0; /* make sure it isn't negative */
  331.  
  332.   voice[i].tremolo_phase=0;
  333.   voice[i].tremolo_phase_increment=voice[i].sample->tremolo_phase_increment;
  334.   voice[i].tremolo_sweep=voice[i].sample->tremolo_sweep_increment;
  335.   voice[i].tremolo_sweep_position=0;
  336.  
  337.   voice[i].vibrato_sweep=voice[i].sample->vibrato_sweep_increment;
  338.   voice[i].vibrato_sweep_position=0;
  339.   voice[i].vibrato_control_ratio=voice[i].sample->vibrato_control_ratio;
  340.   voice[i].vibrato_control_counter=voice[i].vibrato_phase=0;
  341.   for (j=0; j<VIBRATO_SAMPLE_INCREMENTS; j++)
  342.     voice[i].vibrato_sample_increment[j]=0;
  343.  
  344.   if (channel[e->channel].panning != NO_PANNING)
  345.     voice[i].panning=channel[e->channel].panning;
  346.   else
  347.     voice[i].panning=voice[i].sample->panning;
  348.  
  349.   recompute_freq(i);
  350.   recompute_amp(i);
  351.   if (voice[i].sample->modes & MODES_ENVELOPE)
  352.     {
  353.       /* Ramp up from 0 */
  354.       voice[i].envelope_stage=0;
  355.       voice[i].envelope_volume=0;
  356.       voice[i].control_counter=0;
  357.       recompute_envelope(i);
  358.       apply_envelope_to_amp(i);
  359.     }
  360.   else
  361.     {
  362.       voice[i].envelope_increment=0;
  363.       apply_envelope_to_amp(i);
  364.     }
  365.   ctl->note(i);
  366. }
  367.  
  368. static void kill_note(int i)
  369. {
  370.   voice[i].status=VOICE_DIE;
  371.   ctl->note(i);
  372. }
  373.  
  374. /* Only one instance of a note can be playing on a single channel. */
  375. static void note_on(MidiEvent *e)
  376. {
  377.   int i=voices, lowest=-1; 
  378.   int32 lv=0x7FFFFFFF, v;
  379.  
  380.   while (i--)
  381.     {
  382.       if (voice[i].status == VOICE_FREE)
  383.     lowest=i; /* Can't get a lower volume than silence */
  384.       else if (voice[i].channel==e->channel && 
  385.            (voice[i].note==e->a || channel[voice[i].channel].mono))
  386.     kill_note(i);
  387.     }
  388.  
  389.   if (lowest != -1)
  390.     {
  391.       /* Found a free voice. */
  392.       start_note(e,lowest);
  393.       return;
  394.     }
  395.   
  396.   /* Look for the decaying note with the lowest volume */
  397.   i=voices;
  398.   while (i--)
  399.     {
  400.       if ((voice[i].status!=VOICE_ON) &&
  401.       (voice[i].status!=VOICE_DIE))
  402.     {
  403.       v=voice[i].left_mix;
  404.       if ((voice[i].panned==PANNED_MYSTERY) && (voice[i].right_mix>v))
  405.         v=voice[i].right_mix;
  406.       if (v<lv)
  407.         {
  408.           lv=v;
  409.           lowest=i;
  410.         }
  411.     }
  412.     }
  413.  
  414.   if (lowest != -1)
  415.     {
  416.       /* This can still cause a click, but if we had a free voice to
  417.      spare for ramping down this note, we wouldn't need to kill it
  418.      in the first place... Still, this needs to be fixed. Perhaps
  419.      we could use a reserve of voices to play dying notes only. */
  420.       
  421.       cut_notes++;
  422.       voice[lowest].status=VOICE_FREE;
  423.       ctl->note(lowest);
  424.       start_note(e,lowest);
  425.     }
  426.   else
  427.     lost_notes++;
  428. }
  429.  
  430. static void finish_note(int i)
  431. {
  432.   if (voice[i].sample->modes & MODES_ENVELOPE)
  433.     {
  434.       /* We need to get the envelope out of Sustain stage */
  435.       voice[i].envelope_stage=3;
  436.       voice[i].status=VOICE_OFF;
  437.       recompute_envelope(i);
  438.       apply_envelope_to_amp(i);
  439.       ctl->note(i);
  440.     }
  441.   else
  442.     {
  443.       /* Set status to OFF so resample_voice() will let this voice out
  444.          of its loop, if any. In any case, this voice dies when it
  445.          hits the end of its data (ofs>=data_length). */
  446.       voice[i].status=VOICE_OFF;
  447.     }
  448. }
  449.  
  450. static void note_off(MidiEvent *e)
  451. {
  452.   int i=voices;
  453.   while (i--)
  454.     if (voice[i].status==VOICE_ON &&
  455.     voice[i].channel==e->channel &&
  456.     voice[i].note==e->a)
  457.       {
  458.     if (channel[e->channel].sustain)
  459.       {
  460.         voice[i].status=VOICE_SUSTAINED;
  461.         ctl->note(i);
  462.       }
  463.     else
  464.       finish_note(i);
  465.     return;
  466.       }
  467. }
  468.  
  469. /* Process the All Notes Off event */
  470. static void all_notes_off(int c)
  471. {
  472.   int i=voices;
  473.   ctl->cmsg(CMSG_INFO, VERB_DEBUG, "All notes off on channel %d", c);
  474.   while (i--)
  475.     if (voice[i].status==VOICE_ON &&
  476.     voice[i].channel==c)
  477.       {
  478.     if (channel[c].sustain) 
  479.       {
  480.         voice[i].status=VOICE_SUSTAINED;
  481.         ctl->note(i);
  482.       }
  483.     else
  484.       finish_note(i);
  485.       }
  486. }
  487.  
  488. /* Process the All Sounds Off event */
  489. static void all_sounds_off(int c)
  490. {
  491.   int i=voices;
  492.   while (i--)
  493.     if (voice[i].channel==c && 
  494.     voice[i].status != VOICE_FREE &&
  495.     voice[i].status != VOICE_DIE)
  496.       {
  497.     kill_note(i);
  498.       }
  499. }
  500.  
  501. static void adjust_pressure(MidiEvent *e)
  502. {
  503.   int i=voices;
  504.   while (i--)
  505.     if (voice[i].status==VOICE_ON &&
  506.     voice[i].channel==e->channel &&
  507.     voice[i].note==e->a)
  508.       {
  509.     voice[i].velocity=e->b;
  510.     recompute_amp(i);
  511.     apply_envelope_to_amp(i);
  512.     return;
  513.       }
  514. }
  515.  
  516. static void adjust_panning(int c)
  517. {
  518.   int i=voices;
  519.   while (i--)
  520.     if ((voice[i].channel==c) &&
  521.     (voice[i].status==VOICE_ON || voice[i].status==VOICE_SUSTAINED))
  522.       {
  523.     voice[i].panning=channel[c].panning;
  524.     recompute_amp(i);
  525.     apply_envelope_to_amp(i);
  526.       }
  527. }
  528.  
  529. static void drop_sustain(int c)
  530. {
  531.   int i=voices;
  532.   while (i--)
  533.     if (voice[i].status==VOICE_SUSTAINED && voice[i].channel==c)
  534.       finish_note(i);
  535. }
  536.  
  537. static void adjust_pitchbend(int c)
  538. {
  539.   int i=voices;
  540.   while (i--)
  541.     if (voice[i].status!=VOICE_FREE && voice[i].channel==c)
  542.       {
  543.     recompute_freq(i);
  544.       }
  545. }
  546.  
  547. static void adjust_volume(int c)
  548. {
  549.   int i=voices;
  550.   while (i--)
  551.     if (voice[i].channel==c &&
  552.     (voice[i].status==VOICE_ON || voice[i].status==VOICE_SUSTAINED))
  553.       {
  554.     recompute_amp(i);
  555.     apply_envelope_to_amp(i);
  556.       }
  557. }
  558.  
  559. static void seek_forward(int32 until_time)
  560. {
  561.   reset_voices();
  562.   while (current_event->time < until_time)
  563.     {
  564.       switch(current_event->type)
  565.     {
  566.       /* All notes stay off. Just handle the parameter changes. */
  567.  
  568.     case ME_PITCH_SENS:
  569.       channel[current_event->channel].pitchsens=
  570.         current_event->a;
  571.       channel[current_event->channel].pitchfactor=0;
  572.       break;
  573.       
  574.     case ME_PITCHWHEEL:
  575.       channel[current_event->channel].pitchbend=
  576.         current_event->a + current_event->b * 128;
  577.       channel[current_event->channel].pitchfactor=0;
  578.       break;
  579.       
  580.     case ME_MAINVOLUME:
  581.       channel[current_event->channel].volume=current_event->a;
  582.       break;
  583.       
  584.     case ME_PAN:
  585.       channel[current_event->channel].panning=current_event->a;
  586.       break;
  587.           
  588.     case ME_EXPRESSION:
  589.       channel[current_event->channel].expression=current_event->a;
  590.       break;
  591.       
  592.     case ME_PROGRAM:
  593.       if (ISDRUMCHANNEL(current_event->channel))
  594.         /* Change drum set */
  595.         channel[current_event->channel].bank=current_event->a;
  596.       else
  597.         channel[current_event->channel].program=current_event->a;
  598.       break;
  599.  
  600.     case ME_SUSTAIN:
  601.       channel[current_event->channel].sustain=current_event->a;
  602.       break;
  603.  
  604.     case ME_RESET_CONTROLLERS:
  605.       reset_controllers(current_event->channel);
  606.       break;
  607.           
  608.     case ME_TONE_BANK:
  609.       channel[current_event->channel].bank=current_event->a;
  610.       break;
  611.       
  612.     case ME_EOT:
  613.       current_sample=current_event->time;
  614.       return;
  615.     }
  616.       current_event++;
  617.     }
  618.   /*current_sample=current_event->time;*/
  619.   if (current_event != event_list)
  620.     current_event--;
  621.   current_sample=until_time;
  622. }
  623.  
  624. static void skip_to(int32 until_time)
  625. {
  626.   if (current_sample > until_time)
  627.     current_sample=0;
  628.  
  629.   reset_midi();
  630.   buffered_count=0;
  631.   buffer_pointer=common_buffer;
  632.   current_event=event_list;
  633.   
  634.   if (until_time)
  635.     seek_forward(until_time);
  636.   ctl->reset();
  637. }
  638.  
  639. static int apply_controls(void)
  640. {
  641.   int rc, i, did_skip=0;
  642.   int32 val;
  643.   /* ASCII renditions of CD player pictograms indicate approximate effect */
  644.   do
  645.     switch(rc=ctl->read(&val))
  646.       {
  647.       case RC_QUIT: /* [] */
  648.       case RC_LOAD_FILE:      
  649.       case RC_NEXT: /* >>| */
  650.       case RC_REALLY_PREVIOUS: /* |<< */
  651.     play_mode->purge_output();
  652.     return rc;
  653.     
  654.       case RC_CHANGE_VOLUME:
  655.     if (val>0 || amplification > -val)
  656.       amplification += val;
  657.     else 
  658.       amplification=0;
  659.     if (amplification > MAX_AMPLIFICATION)
  660.       amplification=MAX_AMPLIFICATION;
  661.     adjust_amplification();
  662.     for (i=0; i<voices; i++)
  663.       if (voice[i].status != VOICE_FREE)
  664.         {
  665.           recompute_amp(i);
  666.           apply_envelope_to_amp(i);
  667.         }
  668.     ctl->master_volume(amplification);
  669.     break;
  670.  
  671.       case RC_PREVIOUS: /* |<< */
  672.     play_mode->purge_output();
  673.     if (current_sample < 2*play_mode->rate)
  674.       return RC_REALLY_PREVIOUS;
  675.     return RC_RESTART;
  676.  
  677.       case RC_RESTART: /* |<< */
  678.     skip_to(0);
  679.     play_mode->purge_output();
  680.     did_skip=1;
  681.     break;
  682.     
  683.       case RC_JUMP:
  684.     play_mode->purge_output();
  685.     if (val >= sample_count)
  686.       return RC_NEXT;
  687.     skip_to(val);
  688.     return rc;
  689.     
  690.       case RC_FORWARD: /* >> */
  691.     /*play_mode->purge_output();*/
  692.     if (val+current_sample >= sample_count)
  693.       return RC_NEXT;
  694.     skip_to(val+current_sample);
  695.     did_skip=1;
  696.     break;
  697.     
  698.       case RC_BACK: /* << */
  699.     /*play_mode->purge_output();*/
  700.     if (current_sample > val)
  701.       skip_to(current_sample-val);
  702.     else
  703.       skip_to(0); /* We can't seek to end of previous song. */
  704.     did_skip=1;
  705.     break;
  706.       }
  707.   while (rc!= RC_NONE);
  708.  
  709.   /* Advertise the skip so that we stop computing the audio buffer */
  710.   if (did_skip)
  711.     return RC_JUMP; 
  712.   else
  713.     return rc;
  714. }
  715.  
  716. static void do_compute_data(int32 count)
  717. {
  718.   int i;
  719.   memset(buffer_pointer, 0, 
  720.      (play_mode->encoding & PE_MONO) ? (count * 4) : (count * 8));
  721.   for (i=0; i<voices; i++)
  722.     {
  723.       if(voice[i].status != VOICE_FREE)
  724.     mix_voice(buffer_pointer, i, count);
  725.     }
  726.   current_sample += count;
  727. }
  728.  
  729. /* count=0 means flush remaining buffered data to output device, then
  730.    flush the device itself */
  731. static int compute_data(int32 count)
  732. {
  733.   int rc;
  734.   if (!count)
  735.     {
  736.       if (buffered_count)
  737.     play_mode->output_data(common_buffer, buffered_count);
  738.       play_mode->flush_output();
  739.       buffer_pointer=common_buffer;
  740.       buffered_count=0;
  741.       return RC_NONE;
  742.     }
  743.  
  744.   while ((count+buffered_count) >= AUDIO_BUFFER_SIZE)
  745.     {
  746.       do_compute_data(AUDIO_BUFFER_SIZE-buffered_count);
  747.       count -= AUDIO_BUFFER_SIZE-buffered_count;
  748.       play_mode->output_data(common_buffer, AUDIO_BUFFER_SIZE);
  749.       buffer_pointer=common_buffer;
  750.       buffered_count=0;
  751.       
  752.       ctl->current_time(current_sample);
  753.       if ((rc=apply_controls())!=RC_NONE)
  754.     return rc;
  755.     }
  756.   if (count>0)
  757.     {
  758.       do_compute_data(count);
  759.       buffered_count += count;
  760.       buffer_pointer += (play_mode->encoding & PE_MONO) ? count : count*2;
  761.     }
  762.   return RC_NONE;
  763. }
  764.  
  765. int play_midi(MidiEvent *eventlist, int32 events, int32 samples)
  766. {
  767.   int rc;
  768.  
  769.   adjust_amplification();
  770.  
  771.   sample_count=samples;
  772.   event_list=eventlist;
  773.   lost_notes=cut_notes=0;
  774.  
  775.   skip_to(0);
  776.   
  777.   for (;;)
  778.     {
  779. #ifdef __WIN32__
  780.         /* check break signals */ 
  781.         if (intr)
  782.             return RC_QUIT;
  783. #endif
  784.       /* Handle all events that should happen at this time */
  785.       while (current_event->time <= current_sample)
  786.     {
  787.       switch(current_event->type)
  788.         {
  789.  
  790.           /* Effects affecting a single note */
  791.  
  792.         case ME_NOTEON:
  793.           if (!(current_event->b)) /* Velocity 0? */
  794.         note_off(current_event);
  795.           else
  796.         note_on(current_event);
  797.           break;
  798.  
  799.         case ME_NOTEOFF:
  800.           note_off(current_event);
  801.           break;
  802.  
  803.         case ME_KEYPRESSURE:
  804.           adjust_pressure(current_event);
  805.           break;
  806.  
  807.           /* Effects affecting a single channel */
  808.  
  809.         case ME_PITCH_SENS:
  810.           channel[current_event->channel].pitchsens=
  811.         current_event->a;
  812.           channel[current_event->channel].pitchfactor=0;
  813.           break;
  814.           
  815.         case ME_PITCHWHEEL:
  816.           channel[current_event->channel].pitchbend=
  817.         current_event->a + current_event->b * 128;
  818.           channel[current_event->channel].pitchfactor=0;
  819.           /* Adjust pitch for notes already playing */
  820.           adjust_pitchbend(current_event->channel);
  821.           ctl->pitch_bend(current_event->channel, 
  822.                   channel[current_event->channel].pitchbend);
  823.           break;
  824.           
  825.         case ME_MAINVOLUME:
  826.           channel[current_event->channel].volume=current_event->a;
  827.           adjust_volume(current_event->channel);
  828.           ctl->volume(current_event->channel, current_event->a);
  829.           break;
  830.           
  831.         case ME_PAN:
  832.           channel[current_event->channel].panning=current_event->a;
  833.           if (adjust_panning_immediately)
  834.         adjust_panning(current_event->channel);
  835.           ctl->panning(current_event->channel, current_event->a);
  836.           break;
  837.           
  838.         case ME_EXPRESSION:
  839.           channel[current_event->channel].expression=current_event->a;
  840.           adjust_volume(current_event->channel);
  841.           ctl->expression(current_event->channel, current_event->a);
  842.           break;
  843.  
  844.         case ME_PROGRAM:
  845.           if (ISDRUMCHANNEL(current_event->channel))
  846.         {
  847.           /* Change drum set */
  848.           channel[current_event->channel].bank=current_event->a;
  849.         }
  850.           else
  851.         {
  852.           channel[current_event->channel].program=current_event->a;
  853.         }
  854.           ctl->program(current_event->channel, current_event->a);
  855.           break;
  856.  
  857.         case ME_SUSTAIN:
  858.           channel[current_event->channel].sustain=current_event->a;
  859.           if (!current_event->a)
  860.         drop_sustain(current_event->channel);
  861.           ctl->sustain(current_event->channel, current_event->a);
  862.           break;
  863.           
  864.         case ME_RESET_CONTROLLERS:
  865.           reset_controllers(current_event->channel);
  866.           redraw_controllers(current_event->channel);
  867.           break;
  868.  
  869.         case ME_ALL_NOTES_OFF:
  870.           all_notes_off(current_event->channel);
  871.           break;
  872.           
  873.         case ME_ALL_SOUNDS_OFF:
  874.           all_sounds_off(current_event->channel);
  875.           break;
  876.           
  877.         case ME_TONE_BANK:
  878.           channel[current_event->channel].bank=current_event->a;
  879.           break;
  880.  
  881.         case ME_EOT:
  882.           /* Give the last notes a couple of seconds to decay  */
  883.           compute_data(play_mode->rate * 2);
  884.           compute_data(0); /* flush buffer to device */
  885.           ctl->cmsg(CMSG_INFO, VERB_VERBOSE,
  886.            "Playing time: ~%d seconds",
  887.            current_sample/play_mode->rate+2);
  888.           ctl->cmsg(CMSG_INFO, VERB_VERBOSE,
  889.            "Notes cut: %d", cut_notes);
  890.           ctl->cmsg(CMSG_INFO, VERB_VERBOSE,
  891.            "Notes lost totally: %d", lost_notes);
  892.           return RC_TUNE_END;
  893.         }
  894.       current_event++;
  895.     }
  896.  
  897.       rc=compute_data(current_event->time - current_sample);
  898.       /*current_sample=current_event->time;*/
  899.       ctl->refresh();
  900.       if ( (rc!=RC_NONE) && (rc!=RC_JUMP))
  901.       return rc;
  902.     }
  903. }
  904.  
  905.  
  906. int play_midi_file(char *fn)
  907. {
  908.   MidiEvent *event;
  909.   int32 events, samples;
  910.   int rc;
  911.   FILE *fp;
  912.  
  913.   ctl->cmsg(CMSG_INFO, VERB_VERBOSE, "MIDI file: %s", fn);
  914.  
  915.   if (!strcmp(fn, "-"))
  916.     {
  917.       fp=stdin;
  918.       strcpy(current_filename, "(stdin)");
  919.     }
  920.   else if (!(fp=open_file(fn, 1, OF_VERBOSE)))
  921.     return RC_ERROR;
  922.  
  923.   ctl->file_name(current_filename);
  924.  
  925.   event=read_midi_file(fp, &events, &samples);
  926.   
  927.   if (fp != stdin)
  928.       close_file(fp);
  929.   
  930.   if (!event)
  931.       return RC_ERROR;
  932.  
  933.   ctl->cmsg(CMSG_INFO, VERB_NOISY, 
  934.         "%d supported events, %d samples", events, samples);
  935.  
  936.   ctl->total_time(samples);
  937.   ctl->master_volume(amplification);
  938.  
  939.   load_missing_instruments();
  940.   rc=play_midi(event, events, samples);
  941.   if (free_instruments_afterwards)
  942.       free_instruments();
  943.   
  944.   free(event);
  945.   return rc;
  946. }
  947.  
  948. void dumb_pass_playing_list(int number_of_files, char *list_of_files[])
  949. {
  950.     int i=0;
  951.  
  952.     for (;;)
  953.     {
  954.       switch(play_midi_file(list_of_files[i]))
  955.         {
  956.         case RC_REALLY_PREVIOUS:
  957.         if (i>0)
  958.             i--;
  959.         break;
  960.             
  961.         default: /* An error or something */
  962.         case RC_NEXT:
  963.         if (i<number_of_files-1)
  964.             {
  965.             i++;
  966.             break;
  967.             }
  968.         /* else fall through */
  969.         
  970.         case RC_QUIT:
  971.         play_mode->close_output();
  972.         ctl->close();
  973.         return;
  974.         }
  975.     }
  976. }
  977.